package com.jhgsys.internal.workflow.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.workflow.entity.WorkflowNodeReview;
import com.jhgsys.internal.workflow.mapper.WorkflowNodeReviewMapper;
import com.jhgsys.internal.workflow.service.IWorkflowNodeReviewService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 流程节点审批人员表 Service实现
 *
 * @author fenghaijun
 * @date 2024-07-19 09:18:03
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class WorkflowNodeReviewServiceImpl extends ServiceImpl<WorkflowNodeReviewMapper, WorkflowNodeReview> implements IWorkflowNodeReviewService {

    /**
     * 查询（分页）
     * @author fenghaijun
     * @date 2024-07-19 09:18:03
     * @param request QueryRequest
     * @param workflowNodeReview workflowNodeReview
     * @return IPage<WorkflowNodeReview>
    */
    @Override
    public IPage<WorkflowNodeReview> findWorkflowNodeReviews(QueryRequest request, WorkflowNodeReview workflowNodeReview) {
        LambdaQueryWrapper<WorkflowNodeReview> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowNodeReview::getDeletedFlg, FebsConstant.VALID);
        // TODO 设置查询条件

        Page<WorkflowNodeReview> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author fenghaijun
     * @date 2024-07-19 09:18:03
     * @param workflowNodeReview workflowNodeReview
     * @return List<WorkflowNodeReview>
     */
    @Override
    public List<WorkflowNodeReview> findWorkflowNodeReviews(WorkflowNodeReview workflowNodeReview) {
	    LambdaQueryWrapper<WorkflowNodeReview> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowNodeReview::getDeletedFlg, FebsConstant.VALID);
		if(StringUtils.isNotBlank(workflowNodeReview.getTenantId())){
            queryWrapper.eq(WorkflowNodeReview::getTenantId,workflowNodeReview.getTenantId());
        }
        if(StringUtils.isNotBlank(workflowNodeReview.getWorkflowId())){
            queryWrapper.eq(WorkflowNodeReview::getWorkflowId,workflowNodeReview.getWorkflowId());
        }
        if(StringUtils.isNotBlank(workflowNodeReview.getWorkflowNodeId())){
            queryWrapper.eq(WorkflowNodeReview::getWorkflowNodeId,workflowNodeReview.getWorkflowNodeId());
        }
        if(StringUtils.isNotBlank(workflowNodeReview.getWorkflowNodeCheckType())){
            queryWrapper.eq(WorkflowNodeReview::getWorkflowNodeCheckType,workflowNodeReview.getWorkflowNodeCheckType());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author fenghaijun
    * @date 2024-07-19 09:18:03
    * @param id
    * @return WorkflowNodeReview
    */
    @Override
    public WorkflowNodeReview findWorkflowNodeReviewById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author fenghaijun
     * @date 2024-07-19 09:18:03
     * @param workflowNodeReview workflowNodeReview
     */
    @Override
    @Transactional
    public void createWorkflowNodeReview(WorkflowNodeReview workflowNodeReview) {
        workflowNodeReview.setCreatedTime(new Date());
        this.save(workflowNodeReview);
    }

    /**
     * 修改
     * @author fenghaijun
     * @date 2024-07-19 09:18:03
     * @param workflowNodeReview workflowNodeReview
     */
    @Override
    @Transactional
    public void updateWorkflowNodeReview(WorkflowNodeReview workflowNodeReview) {
        workflowNodeReview.setUpdatedTime(new Date());
        this.saveOrUpdate(workflowNodeReview);
    }

    /**
     * 逻辑删除
     * @author fenghaijun
     * @date 2024-07-19 09:18:03
     * @param workflowNodeReview workflowNodeReview
     * @param workflowNodeReviewIds workflowNodeReviewIds
     */
    @Override
    @Transactional
    public void deleteWorkflowNodeReview(WorkflowNodeReview workflowNodeReview, String workflowNodeReviewIds) {
        List<String> list = Arrays.asList(workflowNodeReviewIds.split(StringPool.COMMA));
        this.baseMapper.update(workflowNodeReview,new LambdaQueryWrapper<WorkflowNodeReview>().in(WorkflowNodeReview::getWorkflowNodeReviewId,list));
	}

    /**
    * 物理删除
    * @author fenghaijun
    * @date 2024-07-19 09:18:03
    * @param workflowNodeReviewIds workflowNodeReviewIds
    */
    @Override
    @Transactional
    public void physicallyDeleteWorkflowNodeReview(String workflowNodeReviewIds) {
        List<String> list = Arrays.asList(workflowNodeReviewIds.split(StringPool.COMMA));
        LambdaQueryWrapper<WorkflowNodeReview> wapper = new LambdaQueryWrapper<>();
        wapper.in(WorkflowNodeReview::getWorkflowNodeReviewId,list);
        this.remove(wapper);
    }
}
